﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AuditModel
{
    public class ExpertJudgeBL:IExpertJudgeBL
    {
        private AuditDBEntities db;
        public ExpertJudgeBL()
        {
            db = new AuditDBEntities();
        }
        #region IExpertJudgeBL Members

        /// <summary>
        /// PW+Year+Number.The Year is the system current year
        /// </summary>
        /// <returns></returns>
        public string GetNewID()
        {
            //Get the biggest number.
            int lastNum = 0;
            var nums = (from t in db.ExpertJudges
                        select new { Count = t.SerialNumber.Substring(6) }).ToList();
            if (nums.Count > 0)
            {
                lastNum = nums.Select(x => Convert.ToInt32(x.Count)).Max();
            }
            else
            {
                lastNum = 0;
            }

            return string.Format("PW{0}{1}",DateTime.Now.Year,lastNum+1);
        }

        #endregion

        #region IExpertJudgeBL Members


        public void AddExpertJudge(ExpertJudge e)
        {
            if (e.SerialNumber.Trim() == "")
            {
                throw new Exception("SerialNum is null. Please assign serial number before adding a new expert judge.");
            }
            db.ExpertJudges.AddObject(e);
            db.SaveChanges();
        }

        #endregion

        #region IExpertJudgeBL Members


        /// <summary>
        /// An ExpertJudge may add many Scores to an archive. However, only the last one he give will be effective.
        /// </summary>
        /// <param name="serialNumofExpert"></param>
        /// <returns></returns>
        public Score GetScore(string expertSerialNum)
        {
            var score = (from s in db.Scores
                         where s.ExpertJudgeSerialNumber==expertSerialNum
                         orderby s.SubmitTime descending
                         select s).First();
            return score;
        }

        #endregion

        #region IExpertJudgeBL Members


        public void AddScore(Score score)
        {
            //Check if the SubjectIDs are the same
            int archiveSubjectID = (int)(from a in db.Archives
                                    where a.ID == score.ArchiveID
                                    select a.SubjectID).Single();

            int expertJudgeSubjectID = (int)(from e in db.ExpertJudges
                                             where e.SerialNumber == score.ExpertJudgeSerialNumber
                                             select e.SubjectID).Single();

            if (archiveSubjectID == expertJudgeSubjectID)
            {
                //Check if the sub scores not exceeding percentage setting.
                //db.Archives.Where(a=>a.ID==score.ArchiveID).Single().a

                db.Scores.AddObject(score);
                db.SaveChanges();
            }
            else
            {
                throw new Exception("Archive and ExpertJudge have different SubjectIDs.");
            }
        }

        #endregion
    }
}
